import { getGroupList, addGroup, modifyGroup, delGroup, getRoleList, getRoleList2, getRoleDetail, getSysLogs, getUserByWD, getAuthList, addRole,
        delRole, editRole, getUserList, addUser, editUser, resetUserPSW, changeUserPSW, getUserInfo, freezeUser, unfreezeUser, getUserEditInfo,
        getBranchList, getBranchUser, getBranchUser2, syncBranchUser, authBranchUser } from '@/service/getData'
import { GET_GROUP_LIST, GET_ROLE_LIST, GET_ROLE_DETAIL, GET_SYS_LOGS, GET_USER_AUTOCOMPLETE, GET_AUTH_LIST, GET_USER_LIST, GET_USER_DETAIL } from '@/store/mutation-types'
const state = {
  groups: [], // 架构列表
  roles: [], // 角色列表
  users: { // 用户列表
    page: {}, // 列表
    list: [] // 分页
  }, // 用户列表
  branches: [], // 分部列表
  branchUsers: [], // 分部用户（销司用户）
  authList: [], // 所有权限列表
  logs: { // 日志列表
    page: {}, // 分页
    list: [] // 列表
  },
  roleDetail: {}, // 角色详细信息
  userDetail: { // 用户详细信息
    uid: {},
    avatarUrl: {},
    username: {},
    passwd: {},
    realname: {},
    email: {},
    phone: {},
    groupId: {},
    characterId: {},
    authority: {}
  },
  autoComplete: [] // 用户搜索autocomplete列表
}
const actions = {
  // 架构管理
  async getGroupList ({commit, state}) {
    let res = await getGroupList()
    commit(GET_GROUP_LIST, res.data.data)
    return res
  },
  async addGroup ({commit, state}, payload) {
    let res = await addGroup(payload.name)
    return res
  },
  async modifyGroup ({commit, state}, payload) {
    let res = await modifyGroup(payload.gid, payload.name)
    return res
  },
  async delGroup ({commit, state}, payload) {
    let res = await delGroup(payload.gid)
    return res
  },
  // 角色管理
  async getRoleList ({commit, state}) {
    let res = await getRoleList()
    commit(GET_ROLE_LIST, res.data.data)
    return res
  },
  async getRoleList2 ({commit, state}) {
    let res = await getRoleList2()
    commit(GET_ROLE_LIST, res.data.data)
  },
  async getRoleDetail ({commit, state}, cid) {
    let res = await getRoleDetail(cid)
    commit(GET_ROLE_DETAIL, res.data)
    return res
  },
  async getAuthList ({commit, state}) {
    let res = await getAuthList()
    commit(GET_AUTH_LIST, res.data.data)
    return res
  },
  async addRole ({commit, state}, payload) {
    let res = await addRole(payload.name, payload.authority)
    return res
  },
  async delRole ({commit, state}, cid) {
    let res = await delRole(cid)
    return res
  },
  async editRole ({commit, state}, payload) {
    let res = await editRole(payload.cid, payload.name, payload.authority)
    return res
  },
  // 系统日志
  async getSysLogs ({commit, state}, payload) {
    let res = await getSysLogs(payload)
    if (!res.data.data.list) {
      res.data = {
        page: {},
        list: []
      }
    }
    commit(GET_SYS_LOGS, res.data.data)
    return res
  },
  // 用户管理
  async getAutoComplete ({commit, state}, payload) {
    let res = await getUserByWD(payload.wd, payload.size)
    commit(GET_USER_AUTOCOMPLETE, res.data.data)
    return res
  },
  async getUserList ({commit, state}, payload) {
    let res = await getUserList(payload)
    commit(GET_USER_LIST, res.data.data)
  },
  async getBranchList ({commit, state}, payload) {
    let res = await getBranchList()
    commit('SET_BRANCH_LIST', res.data.data)
  },
  async getBranchUser ({commit, state}, id) {
    let res = await getBranchUser(id)
    commit('SET_BRANCH_USERS', res.data.data)
    return res
  },
  async getBranchUser2 ({commit, state}, payload) {
    let res = await getBranchUser2(payload.username, payload.size, payload.page)
    commit('SET_BRANCH_USERS', res.data.data)
    return res
  },
  async syncBranchUser ({commit, state}, payload) {
    let res = await syncBranchUser()
    return res
  },
  async authBranchUser ({commit, state}, payload) {
    let res = await authBranchUser(payload)
    return res
  },
  async addUser ({commit, state}, payload) {
    let res = await addUser(payload)
    return res
  },
  async editUser ({commit, state}, payload) {
    let res = await editUser(payload)
    return res
  },
  async resetUserPSW ({commit, state}, payload) {
    let res = await resetUserPSW(payload.uid, payload.passwd)
    return res
  },
  async changeUserPSW ({commit, state}, payload) {
    let res = await changeUserPSW(payload.old, payload.new)
    return res
  },
  async getUserDetail ({commit, state}, uid) {
    let res = await getUserInfo(uid)
    commit(GET_USER_DETAIL, res.data.data)
    return res
  },
  async getUserDetail2 ({commit, state}, uid) {
    const res = await getUserEditInfo(uid)
    commit(GET_USER_DETAIL, res.data.data)
    return res
  },
  async freezeUser ({commit, state}, uid) {
    return await freezeUser(uid)
  },
  async unfreezeUser ({commit, state}, uid) {
    return await unfreezeUser(uid)
  }
}
const mutations = {
  [GET_GROUP_LIST] (state, list) {
    state.groups = list
  },
  [GET_ROLE_LIST] (state, list) {
    state.roles = list
  },
  [GET_ROLE_DETAIL] (state, payload) {
    state.roleDetail = payload
  },
  [GET_SYS_LOGS] (state, payload) {
    state.logs = payload
  },
  [GET_USER_AUTOCOMPLETE] (state, payload) {
    state.autoComplete = payload
  },
  [GET_AUTH_LIST] (state, payload) {
    state.authList = payload
  },
  [GET_USER_LIST] (state, payload) {
    state.users = payload
  },
  [GET_USER_DETAIL] (state, payload) {
    state.userDetail = payload
  },
  ['SET_BRANCH_LIST'] (state, payload) {
    state.branches = payload
  },
  ['SET_BRANCH_USERS'] (state, payload) {
    state.branchUsers = payload
  }
}
export default {
  state,
  actions,
  mutations
}
